﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKExcelFormTool.Analyser;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class SelectTableHeaderForm : System.Windows.Forms.Form
	{
		public TableHead SelectTableHead
		{
			get
			{
				return this.m_HeadList[this.m_SelectHeadIndex];
			}
		}

		public SelectTableHeaderForm(ExternalCommandData commdData, List<SymbolTypeHeadInfos> headerTypeInfos, List<string> properties, string fontName, double fontHeight, double fontWidthScale)
		{
			this.m_Revit = commdData;
			this.m_Properties = properties;
			this.m_ShowMapping = true;
			this.InitializeComponent();
			foreach (SymbolTypeHeadInfos symbolTypeHeadInfos in headerTypeInfos)
			{
				this.m_SymbolTypeIdList.Add(symbolTypeHeadInfos.m_SymbolId);
				this.cmbHeaderList.Items.Add(symbolTypeHeadInfos.m_Name);
				this.m_HeadList.Add(new TableHead());
			}
			this.cmbHeaderList.SelectedIndex = 0;
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
			this.m_FontWidthScale = fontWidthScale;
			this.m_Previewer = new Previewer(this, this, this.panelPreview.Left, this.panelPreview.Top, this.panelPreview.Width, this.panelPreview.Height, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		public SelectTableHeaderForm(ExternalCommandData commdData, List<SymbolTypeHeadInfos> headerTypeInfos, string fontName, double fontHeight, double fontWidthScale)
		{
			this.m_Revit = commdData;
			this.m_ShowMapping = false;
			this.InitializeComponent();
			foreach (SymbolTypeHeadInfos symbolTypeHeadInfos in headerTypeInfos)
			{
				this.m_SymbolTypeIdList.Add(symbolTypeHeadInfos.m_SymbolId);
				this.cmbHeaderList.Items.Add(symbolTypeHeadInfos.m_Name);
				this.m_HeadList.Add(new TableHead());
			}
			this.cmbHeaderList.SelectedIndex = 0;
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
			this.m_FontWidthScale = fontWidthScale;
			this.m_Previewer = new Previewer(this, this, this.panelPreview.Left, this.panelPreview.Top, this.panelPreview.Width, this.panelPreview.Height, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		private void cmbHeaderList_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectHeadIndex = this.cmbHeaderList.SelectedIndex;
			this.ChangeHeadInfos();
			this.ReadColumnMappingInfo(this.m_HeadList[this.m_SelectHeadIndex]);
			this.SetColMappint();
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() == 1)
				{
					if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
					{
						return;
					}
					TableDrawingKitForPicture tableDrawingKitForPicture = new TableDrawingKitForPicture(this.m_HeadList[this.m_SelectHeadIndex], this.m_FontHeight);
					try
					{
						tableDrawingKitForPicture.Drawing();
					}
					catch (Exception)
					{
					}
					this.m_Previewer.Curves = tableDrawingKitForPicture.Curves;
					this.m_Previewer.TextNotes = tableDrawingKitForPicture.Texts;
					this.m_Previewer.FontName = this.m_FontName;
					this.m_Previewer.FontHeight = Common.MMToFeet(this.m_FontHeight);
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private void dbgPropertiesMapping_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.ComboBox comboBox = e.Control as System.Windows.Forms.ComboBox;
			if (comboBox != null)
			{
				comboBox.SelectedIndexChanged += this.DGVComboBoxSelectionChanged;
			}
		}

		private void SelectTableHeaderForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_ShowMapping)
			{
				TableHead tableHead = this.m_HeadList[this.m_SelectHeadIndex];
				HeadColumnMappingScheme headColumnMappingScheme = new HeadColumnMappingScheme(tableHead.TableSourceInfo.Name, tableHead.TableSourceInfo.SheetIndex);
				foreach (TableHeadCell tableHeadCell in tableHead.GetEffectiveRow())
				{
					headColumnMappingScheme.AddMappingItem(new KeyValuePair<string, string>(tableHeadCell.Value, tableHeadCell.Key.KeyName));
				}
				TableHeadColumnMappingStorage.WriteColumnMappingInfo(headColumnMappingScheme);
			}
		}

		private void DGVComboBoxSelectionChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgPropertiesMapping.CurrentCell.RowIndex;
			string text = ((System.Windows.Forms.ComboBox)sender).Text;
			this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[rowIndex].Key = new TableHeadCellKey(text);
		}

		private void ChangeHeadInfos()
		{
			if (this.m_HeadList[this.m_SelectHeadIndex].IsNull())
			{
				Family family = (RevitVersionFuncs.GetElement(this.m_Revit.Application.ActiveUIDocument.Document, this.m_SymbolTypeIdList[this.m_SelectHeadIndex]) as AnnotationSymbolType).Family;
				Document document = this.m_Revit.Application.ActiveUIDocument.Document.EditFamily(family);
				if (document == null || !document.IsFamilyDocument)
				{
					YJKMessageBox.Show("不是一个族文件");
					return;
				}
				IList<Element> list = new FilteredElementCollector(document).OfClass(typeof(CurveElement)).ToElements();
				List<DetailLine> list2 = new List<DetailLine>();
				for (int i = 0; i < list.Count; i++)
				{
					DetailLine detailLine = list[i] as DetailLine;
					if (detailLine != null)
					{
						list2.Add(detailLine);
					}
				}
				IList<Element> list3 = new FilteredElementCollector(document).OfClass(typeof(TextElement)).ToElements();
				List<TextElement> list4 = new List<TextElement>();
				for (int i = 0; i < list3.Count; i++)
				{
					TextElement textElement = list3[i] as TextElement;
					if (textElement != null)
					{
						list4.Add(textElement);
					}
				}
				FamilyTableAnalyser familyTableAnalyser = new FamilyTableAnalyser(this.m_Revit, document);
				string text = "";
				familyTableAnalyser.Analyse(list2, list4, ref text);
				this.m_HeadList[this.m_SelectHeadIndex] = familyTableAnalyser.TheTableHead;
			}
			this.Refresh();
		}

		private void SetColMappint()
		{
			if (!this.m_ShowMapping)
			{
				return;
			}
			this.dbgPropertiesMapping.Rows.Clear();
			List<string> list = new List<string>();
			foreach (string item in this.m_Properties)
			{
				list.Add(item);
			}
			list.Add("");
			List<TableHeadCell> effectiveRow = this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow();
			int i = 0;
			int num = 1;
			while (i < effectiveRow.Count)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = effectiveRow[i].Value;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
				foreach (string item2 in list)
				{
					dataGridViewComboBoxCell.Items.Add(item2);
				}
				string keyName = this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[i].Key.KeyName;
				int num2 = list.FindIndex((string s) => s.CompareTo(keyName) == 0);
				if (-1 != num2 && keyName.CompareTo("") != 0)
				{
					dataGridViewComboBoxCell.Value = keyName;
				}
				else
				{
					dataGridViewComboBoxCell.Value = list[num - 1];
					this.m_HeadList[this.m_SelectHeadIndex].GetEffectiveRow()[i].Key = new TableHeadCellKey(list[num - 1]);
				}
				dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
				this.dbgPropertiesMapping.Rows.Add(dataGridViewRow);
				num++;
				i++;
			}
			if (effectiveRow.Count > 8)
			{
				this.cMainSpaceCategory.Width = 100;
			}
		}

		private void ReadColumnMappingInfo(TableHead head)
		{
			if (!this.m_ShowMapping)
			{
				return;
			}
			HeadColumnMappingScheme headColumnMappingScheme = new HeadColumnMappingScheme(head.TableSourceInfo.Name, head.TableSourceInfo.SheetIndex);
			if (headColumnMappingScheme.SourceName.CompareTo("") == 0)
			{
				return;
			}
			if (TableHeadColumnMappingStorage.ReadColumnMappingInfo(ref headColumnMappingScheme))
			{
				List<TableHeadCell> effectiveRow = head.GetEffectiveRow();
				for (int i = 0; i < effectiveRow.Count; i++)
				{
					string name = head.GetEffectiveRow()[i].Value;
					int num = headColumnMappingScheme.MappingInfo.FindIndex((KeyValuePair<string, string> s) => s.Key.CompareTo(name) == 0);
					if (-1 != num)
					{
						head.GetEffectiveRow()[i].Key = new TableHeadCellKey(headColumnMappingScheme.MappingInfo[num].Value);
					}
				}
			}
		}

		private void SelectTableHeaderForm_Load(object sender, EventArgs e)
		{
			if (!this.m_ShowMapping)
			{
				this.btnOk.Left = 358;
				this.btnCancel.Left = 436;
				base.Width = 535;
				this.dbgPropertiesMapping.Visible = false;
				return;
			}
			this.ReadColumnMappingInfo(this.m_HeadList[this.m_SelectHeadIndex]);
			this.SetColMappint();
		}

		private ExternalCommandData m_Revit;

		private List<ElementId> m_SymbolTypeIdList = new List<ElementId>();

		private List<TableHead> m_HeadList = new List<TableHead>();

		private List<string> m_Properties = new List<string>();

		private int m_SelectHeadIndex;

		private string m_FontName = "";

		private double m_FontHeight;

		private double m_FontWidthScale = 1.0;

		private bool m_ShowMapping;

		private Previewer m_Previewer;
	}
}
